<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
      <!-- <el-form-item label="上级学校id，用于表示学校之间的层级关系，0表示没有上级" prop="parentId">
        <el-input
          v-model="queryParams.parentId"
          placeholder="请输入上级学校id，用于表示学校之间的层级关系，0表示没有上级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="祖级列表，用于快速查询某个学校的所有上级学校" prop="ancestors">
        <el-input
          v-model="queryParams.ancestors"
          placeholder="请输入祖级列表，用于快速查询某个学校的所有上级学校"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="名称" prop="schoolName">
        <el-input
          v-model="queryParams.schoolName"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="显示顺序，用于列表或树状结构中的排序" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入显示顺序，用于列表或树状结构中的排序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="负责人" prop="principal">
        <el-input
          v-model="queryParams.principal"
          placeholder="请输入负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="邮箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['business:school:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="schoolList"
      row-key="schoolId"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="学校名称" prop="schoolName" />
      <!-- <el-table-column label="祖级列表，用于快速查询某个学校的所有上级学校" align="center" prop="ancestors" /> -->
      <!-- <el-table-column label="上级学校id，用于表示学校之间的层级关系，0表示没有上级" align="center" prop="schoolId" /> -->
      <!-- <el-table-column label="显示顺序，用于列表或树状结构中的排序" align="center" prop="orderNum" /> -->
      <el-table-column label="负责人" align="center" prop="principal" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="邮箱" align="center" prop="email" />
      <!-- <el-table-column label="学校状态" align="center" prop="status" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['business:school:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['com/ruoyi/business:school:add']"
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
          >新增</el-button>
          <el-button
            v-hasPermi="['business:school:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改学校信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="上级" prop="parentId">
          <treeselect v-model="form.parentId" :options="schoolOptions" :normalizer="normalizer" placeholder="请选择上级" />
        </el-form-item> -->
        <el-form-item label="学校名称" prop="schoolName">
          <el-input v-model="form.schoolName" placeholder="请输入学校名称" />
        </el-form-item>
        <!-- <el-form-item label="显示顺序" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入显示顺序" />
        </el-form-item> -->
        <el-form-item label="负责人" prop="principal">
          <el-input v-model="form.principal" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <!-- <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSchool, getSchool, delSchool, addSchool, updateSchool } from '@/api/schoolSystem/schoolMessageList'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'School',
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 学校信息表格数据
      schoolList: [],
      // 学校信息树选项
      schoolOptions: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        parentId: null,
        ancestors: null,
        schoolName: null,
        orderNum: null,
        principal: null,
        phone: null,
        email: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        schoolName:[
          { required: true, message: "学校名称不能为空", trigger: "blur" }
        ],
        principal:[
          { required: true, message: "负责人不能为空", trigger: "blur" }
        ],
        phone:[
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        email:[
          { required: true, message: "邮箱不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询学校信息列表 */
    getList() {
      this.loading = true
      listSchool(this.queryParams).then(response => {
        this.schoolList = this.handleTree(response.data, 'schoolId', 'parentId')
        this.loading = false
      })
    },
    /** 转换学校信息数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.schoolId,
        label: node.schoolName,
        children: node.children
      }
    },
    /** 查询学校信息下拉树结构 */
    getTreeselect() {
      listSchool().then(response => {
        this.schoolOptions = []
        const data = { schoolId: 0, schoolName: '顶级节点', children: [] }
        data.children = this.handleTree(response.data, 'schoolId', 'parentId')
        this.schoolOptions.push(data)
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        schoolId: null,
        parentId: null,
        ancestors: null,
        schoolName: null,
        orderNum: null,
        principal: null,
        phone: null,
        email: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset()
      this.getTreeselect()
      if (row != null && row.schoolId) {
        this.form.parentId = row.schoolId
      } else {
        this.form.parentId = 0
      }
      this.open = true
      this.title = '添加学校信息'
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false
      this.isExpandAll = !this.isExpandAll
      this.$nextTick(() => {
        this.refreshTable = true
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.getTreeselect()
      // console.log("=======row",row)
      if (row != null) {
        this.form.parentId = row.parentId
      }
      getSchool(row.schoolId).then(response => {
        this.form = response.data
        this.open = true
        // console.log('9999======', response.data.ancestors)
        if(response.data.ancestors == "0"){
          this.title = '修改学校信息'
          return
        }else if(response.data.ancestors == "0,100"){
          this.title = '修改年级信息'
          return
        }else if(response.data.ancestors == "0,100,101"){
          this.title = '修改班级信息'
          return
        }else{
          this.title = '修改信息'
        }

      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.schoolId != null) {
            updateSchool(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addSchool(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除学校信息编号为"' + row.schoolId + '"的数据项？').then(function() {
        return delSchool(row.schoolId)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    }
  }
}
</script>
